package casebookcontroller

import (
	"system_go/models"
	"system_go/pkg/app"
	"system_go/pkg/e"

	"github.com/gin-gonic/gin"
	"github.com/unknwon/com"
)

// 获取所有病例
func GetAllCaseBook(c *gin.Context) {
	pagenum := -1
	if arg := c.Query("pagenum"); arg != "" {
		pagenum = com.StrTo(arg).MustInt()
	}
	pagesize := -1
	if arg := c.Query("pagesize"); arg != "" {
		pagesize = com.StrTo(arg).MustInt()
	}
	query := c.Query("query")
	studentParam := map[string]interface{}{
		"pagenum":  pagenum,
		"pagesize": pagesize,
		"query":    query,
	}

	info, total, err := models.GetAllCaseBook(studentParam)
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	app.OK(c, map[string]interface{}{"value": info, "total": total, "query": query, "pagenum": pagenum, "pagesize": pagesize}, "OK")
}

// 增加病例的控制器
func AddCaseBook(c *gin.Context) {
	info, total, err := models.AddCaseBook(c)
	if total != 0 {
		app.Error(c, e.ERROR_UPLOAD_CHECK_IMAGE_FAIL, err, "not ok")
		return
	}
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	app.OK(c, info, "OK")
}

// 更新病例的控制器
func UpdateCaseBook(c *gin.Context) {
	info, err := models.UpdateCaseBook(c)
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	app.OK(c, info, "OK")
}

// 删除商品的控制器
func DelCaseBook(c *gin.Context) {
	err := models.DelCaseBook(c)
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	app.OK(c, "student deleted successfully", "OK")
}
